#include "net_cli_command.h"
// 假设的FreeRTOS类型定义
//typedef int BaseType_t;
//typedef unsigned int UBaseType_t;
//typedef unsigned char uint8_t;
//typedef unsigned short uint16_t;
//#define pdFALSE 0
//#define pdTRUE  1

// 假设的网络接口配置结构体

// 假设的CLI参数获取函数
const char *FreeRTOS_CLIGetParameter(const char *pcCommandString, UBaseType_t uxWantedParameter, BaseType_t *pxParameterStringLength);

// Helper function: Convert IP array to string
void ipv4_to_string(const uint8_t *ip, char *str, size_t str_size)
{
  snprintf(str, str_size, "%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
}

void ipv6_to_string(const uint8_t *ipv6, char *str, size_t str_size)
{
  snprintf(str, str_size, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
           ipv6[0], ipv6[1], ipv6[2], ipv6[3], ipv6[4], ipv6[5], ipv6[6], ipv6[7],
           ipv6[8], ipv6[9], ipv6[10], ipv6[11], ipv6[12], ipv6[13], ipv6[14], ipv6[15]);
}

// Helper function: Convert MAC array to string
void mac_to_string(const uint8_t *mac, char *str, size_t str_size)
{
  snprintf(str, str_size, "%02x:%02x:%02x:%02x:%02x:%02x",
           mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
}

// Helper function: Convert IP string to array
bool string_to_ipv4(const char *str, uint8_t *ip)
{
  if (!str || !ip)
    return false;

  int a, b, c, d;
  if (sscanf(str, "%d.%d.%d.%d", &a, &b, &c, &d) != 4)
  {
    return false;
  }

  if (a < 0 || a > 255 || b < 0 || b > 255 || c < 0 || c > 255 || d < 0 || d > 255)
  {
    return false;
  }

  ip[0] = (uint8_t)a;
  ip[1] = (uint8_t)b;
  ip[2] = (uint8_t)c;
  ip[3] = (uint8_t)d;
  return true;
}

// Helper function: Convert MAC string to array
bool string_to_mac(const char *str, uint8_t *mac)
{
  if (!str || !mac)
    return false;

  int a, b, c, d, e, f;
  if (sscanf(str, "%x:%x:%x:%x:%x:%x", &a, &b, &c, &d, &e, &f) != 6)
  {
    return false;
  }

  if (a < 0 || a > 255 || b < 0 || b > 255 || c < 0 || c > 255 ||
      d < 0 || d > 255 || e < 0 || e > 255 || f < 0 || f > 255)
  {
    return false;
  }

  mac[0] = (uint8_t)a;
  mac[1] = (uint8_t)b;
  mac[2] = (uint8_t)c;
  mac[3] = (uint8_t)d;
  mac[4] = (uint8_t)e;
  mac[5] = (uint8_t)f;
  return true;
}
int get_dhcp_status(char *dhcp_status, int u_status)
{
  if (dhcp_status == NULL)
  {
    return -1;
  }
  switch (u_status)
  {
  case 1:
    strcpy(dhcp_status, "success");
    break;
  case 0:
    strcpy(dhcp_status, "disabled");
    break;
  case -2:
    strcpy(dhcp_status, "timeout");
    break;
  case -4:
    strcpy(dhcp_status, "conflict");
    break;
  default:
    strcpy(dhcp_status, "unknown");
    break;
  }
  return 0;
}

// Network CLI command function
// Network CLI command function
BaseType_t network_cli_command(char *pcWriteBuffer, size_t xWriteBufferLen,
                               const char *pcCommandString)
{
  char eth0_dhcp[16], eth1_dhcp[16];
  const char *pcSub = FreeRTOS_CLIGetParameter(pcCommandString, 1, &(BaseType_t){0});
  const char *pcInterface = FreeRTOS_CLIGetParameter(pcCommandString, 2, &(BaseType_t){0});
  const char *pcAction = FreeRTOS_CLIGetParameter(pcCommandString, 3, &(BaseType_t){0});
  const char *pcFieldType = FreeRTOS_CLIGetParameter(pcCommandString, 4, &(BaseType_t){0});
  const char *pcValue = FreeRTOS_CLIGetParameter(pcCommandString, 5, &(BaseType_t){0});

  // 根据实际解析结果进行处理
  // 对于 "net show eth0"，pcSub = "show eth0", pcInterface = "eth0"
  // 需要先检查pcSub是否以"show"开头
  if (pcSub && strncmp(pcSub, "show", 4) == 0)
  {
    // 如果pcSub包含多个词，需要进一步解析
    char *space_pos = strchr(pcSub, ' ');
    char *interface = NULL;
    get_dhcp_status(eth0_dhcp, net_config->eth0_dhcp_flag);
    get_dhcp_status(eth1_dhcp, net_config->eth1_dhcp_flag);

    if (space_pos != NULL)
    {
      // "show eth0" 格式，pcSub = "show eth0"
      interface = space_pos + 1;
    }
    else
    {
      // 只有 "show"，没有接口
      interface = (char *)pcInterface; // 尝试从下一个参数获取
    }

    if (!interface)
    {
      // No interface specified, show help
      snprintf(pcWriteBuffer, xWriteBufferLen,
               "\r\nUsage: net show eth0/eth1/all\r\n"
               "Examples:\r\n"
               "  net show eth0\r\n"
               "  net show eth1\r\n"
               "  net show all\r\n");
      return pdFALSE;
    }

    // Check if it's "net show all"
    if (strcmp(interface, "all") == 0)
    {
      char eth0_ip[16], eth1_ip[16];
      char eth0_ipmask[16], eth1_ipmask[16];
      char eth0_gw[16], eth1_gw[16];
      char eth0_mac[18], eth1_mac[18];
      ipv4_to_string(net_config->eth0_ipv4, eth0_ip, sizeof(eth0_ip));
      ipv4_to_string(net_config->eth1_ipv4, eth1_ip, sizeof(eth1_ip));
      ipv4_to_string(net_config->eth0_ipv4_netmask, eth0_ipmask, sizeof(eth0_ipmask));
      ipv4_to_string(net_config->eth1_ipv4_netmask, eth1_ipmask, sizeof(eth1_ipmask));
      ipv4_to_string(net_config->eth0_ipv4_gw, eth0_gw, sizeof(eth0_gw));
      ipv4_to_string(net_config->eth1_ipv4_gw, eth1_gw, sizeof(eth1_gw));
      mac_to_string(net_config->eth0_mac, eth0_mac, sizeof(eth0_mac));
      mac_to_string(net_config->eth1_mac, eth1_mac, sizeof(eth1_mac));
      snprintf(pcWriteBuffer, xWriteBufferLen,
               "\r\n=== Network Interface Overview ===\r\n"
               "ETH0:\r\n"
               "  MAC Address: %s\r\n"
               "  Link Status: %s\r\n"
               "  Connection Speed: %d Mbps\r\n"
               "  Full Duplex: %s\r\n"
               "  DHCP: %s\r\n"
               "  IPv4 Address: %s\r\n"
               "  Subnet Mask: %s\r\n"
               "  Gateway Address: %s\r\n"
               "ETH1:\r\n"
               "  MAC Address: %s\r\n"
               "  Link Status: %s\r\n"
               "  Connection Speed: %d Mbps\r\n"
               "  Full Duplex: %s\r\n"
               "  DHCP: %s\r\n"
               "  IPv4 Address: %s\r\n"
               "  Subnet Mask: %s\r\n"
               "  Gateway Address: %s\r\n",
               eth0_mac,
               net_config->eth0_link_up ? "UP" : "DOWN",
               net_config->eth0_speed,
               net_config->eth0_full_duplex ? "Yes" : "No",
               eth0_dhcp,
               eth0_ip, eth0_ipmask, eth0_gw,
               eth1_mac,
               net_config->eth1_link_up ? "UP" : "DOWN",
               net_config->eth1_speed,
               net_config->eth1_full_duplex ? "Yes" : "No",
               eth1_dhcp,
               eth1_ip, eth1_ipmask, eth1_gw);
      return pdFALSE;
    }
    // Handle "net show eth0" or "net show eth1"
    else if (strcmp(interface, "eth0") == 0)
    {
      char ip_str[16], mask_str[16], gw_str[16], dns_str[16];
      char lla_str[40], gua_str[40], sn6_str[40], gw6_str[40];
      char mac_str[18];

      ipv4_to_string(net_config->eth0_ipv4, ip_str, sizeof(ip_str));
      ipv4_to_string(net_config->eth0_ipv4_netmask, mask_str, sizeof(mask_str));
      ipv4_to_string(net_config->eth0_ipv4_gw, gw_str, sizeof(gw_str));
      ipv4_to_string(net_config->eth0_ipv4_dns, dns_str, sizeof(dns_str));
      ipv6_to_string(net_config->eth0_lla, lla_str, sizeof(lla_str));
      ipv6_to_string(net_config->eth0_gua, gua_str, sizeof(gua_str));
      ipv6_to_string(net_config->eth0_sn6, sn6_str, sizeof(sn6_str));
      ipv6_to_string(net_config->eth0_gw6, gw6_str, sizeof(gw6_str));
      mac_to_string(net_config->eth0_mac, mac_str, sizeof(mac_str));

      snprintf(pcWriteBuffer, xWriteBufferLen,
               "\r\n=== ETH0 Network Status ===\r\n"
               "MAC Address: %s\r\n"
               "Link Status: %s\r\n"
               "Connection Speed: %d Mbps\r\n"
               "Full Duplex Mode: %s\r\n"
               "DHCP: %s\r\n"
               "IPv4 Address: %s\r\n"
               "Subnet Mask: %s\r\n"
               "Gateway Address: %s\r\n"
               "DNS Server: %s\r\n"
               "LLA IPv6: %s\r\n"
               "GUA IPv6: %s\r\n"
               "SN6: %s\r\n"
               "GW6: %s\r\n"
               "IPv6 DHCP: %s\r\n",
               mac_str,
               net_config->eth0_link_up ? "UP" : "DOWN",
               net_config->eth0_speed,
               net_config->eth0_full_duplex ? "Yes" : "No",
               eth0_dhcp,
               ip_str, mask_str, gw_str, dns_str,
               lla_str, gua_str, sn6_str, gw6_str,
               net_config->eth0_en_ipv6_dhcp_flag ? "Enabled" : "Disabled");
      return pdFALSE;
    }
    else if (strcmp(interface, "eth1") == 0)
    {
      char ip_str[16], mask_str[16], gw_str[16], dns_str[16];
      char lla_str[40], gua_str[40], sn6_str[40], gw6_str[40];
      char mac_str[18];

      ipv4_to_string(net_config->eth1_ipv4, ip_str, sizeof(ip_str));
      ipv4_to_string(net_config->eth1_ipv4_netmask, mask_str, sizeof(mask_str));
      ipv4_to_string(net_config->eth1_ipv4_gw, gw_str, sizeof(gw_str));
      ipv4_to_string(net_config->eth1_ipv4_dns, dns_str, sizeof(dns_str));
      ipv6_to_string(net_config->eth1_lla, lla_str, sizeof(lla_str));
      ipv6_to_string(net_config->eth1_gua, gua_str, sizeof(gua_str));
      ipv6_to_string(net_config->eth1_sn6, sn6_str, sizeof(sn6_str));
      ipv6_to_string(net_config->eth1_gw6, gw6_str, sizeof(gw6_str));
      mac_to_string(net_config->eth1_mac, mac_str, sizeof(mac_str));

      snprintf(pcWriteBuffer, xWriteBufferLen,
               "\r\n=== ETH1 Network Status ===\r\n"
               "MAC Address: %s\r\n"
               "Link Status: %s\r\n"
               "Connection Speed: %d Mbps\r\n"
               "Full Duplex Mode: %s\r\n"
               "DHCP: %s\r\n"
               "IPv4 Address: %s\r\n"
               "Subnet Mask: %s\r\n"
               "Gateway Address: %s\r\n"
               "DNS Server: %s\r\n"
               "LLA IPv6: %s\r\n"
               "GUA IPv6: %s\r\n"
               "SN6: %s\r\n"
               "GW6: %s\r\n"
               "IPv6 DHCP: %s\r\n",
               mac_str,
               net_config->eth1_link_up ? "UP" : "DOWN",
               net_config->eth1_speed,
               net_config->eth1_full_duplex ? "Yes" : "No",
               eth1_dhcp,
               ip_str, mask_str, gw_str, dns_str,
               lla_str, gua_str, sn6_str, gw6_str,
               net_config->eth1_en_ipv6_dhcp_flag ? "Enabled" : "Disabled");
      return pdFALSE;
    }
    else
    {
      snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nError: Invalid network interface. Please use eth0 or eth1 or all\r\n");
      return pdFALSE;
    }
  }

  // Check if the command starts with "set"
  // 对于 "set" 命令，根据你提供的实际解析结果进行处理
  else if (pcSub && strncmp(pcSub, "set", 3) == 0)
  {
    // 根据实际运行结果，对于 "net set eth0 dhcp off"：
    // pcSub = "set eth0 dhcp on", pcInterface = "eth0 dhcp on", pcAction = "dhcp on"
    // 需要手动解析这些参数
    char temp_cmd[256];
    strcpy(temp_cmd, pcCommandString);

    // 手动解析命令：net set eth0 dhcp off
    char *token = strtok(temp_cmd, " ");
    if (token == NULL)
    {
      snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nError: Invalid command format\r\n");
      return pdFALSE;
    }

    // 跳过 "net"
    token = strtok(NULL, " ");
    if (token == NULL || strcmp(token, "set") != 0)
    {
      snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nError: Invalid command format\r\n");
      return pdFALSE;
    }

    // 获取接口名
    token = strtok(NULL, " ");
    if (token == NULL)
    {
      snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nError: No interface specified\r\n");
      return pdFALSE;
    }
    char *interface = token;

    // 获取动作
    token = strtok(NULL, " ");
    if (token == NULL)
    {
      snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nError: No action specified\r\n");
      return pdFALSE;
    }
    char *action = token;

    // 获取值
    token = strtok(NULL, " ");
    char *value = token; // 可能为NULL

    if (strcmp(interface, "eth0") == 0)
    {
      // Set MAC address
      if (strcmp(action, "mac") == 0 && value)
      {
        if (string_to_mac(value, net_config->eth0_mac))
        {
          char mac_str[18];
          setting_para->port1_use_written_mac = 1;
          memcpy(setting_para->port1_written_mac, net_config->eth0_mac, 6);
          write_base_paremeter_to_fs(setting_para);
          mac_to_string(net_config->eth0_mac, mac_str, sizeof(mac_str));
          snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nETH0 MAC address has been set to %s\r\n", mac_str);
        }
        else
        {
          setting_para->port1_use_written_mac = 0;
          write_base_paremeter_to_fs(setting_para);
          snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nETH0 MAC address is set to auto-generated. Please restart the device.\r\n");
          snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nError: Invalid MAC address format. Please use format: aa:bb:cc:dd:ee:ff\r\n");
        }
        return pdFALSE;
      }
      // Set DHCP
      else if (strcmp(action, "dhcp") == 0 && value)
      {
        if (strcmp(value, "on") == 0 || strcmp(value, "1") == 0)
        {
          net_config->eth0_dhcp_flag = 1;
          setting_para->port1_DHCPV4_FLAG = 1;
          write_base_paremeter_to_fs(setting_para);
          w6100_net_status = W6100_INIT;
          MQTT_connet_status = MQTT_DISCONNECT;
          snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nETH0 DHCP has been enabled\r\n");
        }
        else if (strcmp(value, "off") == 0 || strcmp(value, "0") == 0)
        {
          net_config->eth0_dhcp_flag = 0;
          setting_para->port1_DHCPV4_FLAG = 0;
          write_base_paremeter_to_fs(setting_para);
          w6100_net_status = W6100_INIT;
          snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nETH0 DHCP has been disabled\r\n");
        }
        else
        {
          snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nError: Invalid DHCP value. Please use on/off or 1/0\r\n");
        }
        return pdFALSE;
      }
      // Set IPv4 parameters
      else if (strcmp(action, "ipv4") == 0 && value)
      {
        // 需要再次获取参数类型
        char temp_cmd2[256];
        strcpy(temp_cmd2, pcCommandString);

        token = strtok(temp_cmd2, " ");
        // 跳过 "net"
        token = strtok(NULL, " ");
        // 跳过 "set"
        token = strtok(NULL, " ");
        // 跳过 interface
        token = strtok(NULL, " ");
        // 跳过 action (ipv4)
        token = strtok(NULL, " ");
        // 现在token是参数类型 (ip, ipmask, gw, dns)
        char *param_type = token;

        // 获取参数值
        token = strtok(NULL, " ");
        char *param_value = token;

        if (param_type && param_value)
        {
          if (strcmp(param_type, "ip") == 0)
          {
            if (string_to_ipv4(param_value, net_config->eth0_ipv4))
            {
              setting_para->port1_DHCPV4_FLAG = 0;
              memcpy(setting_para->port1_IPV4, net_config->eth0_ipv4, 4);
              write_base_paremeter_to_fs(setting_para);
              w6100_net_status = W6100_INIT;
              MQTT_connet_status = MQTT_DISCONNECT;
              snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nETH0 IPv4 address has been set to %s\r\n", param_value);
            }
            else
            {
              snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nError: Invalid IPv4 address format\r\n");
            }
            return pdFALSE;
          }
          else if (strcmp(param_type, "ipmask") == 0)
          {
            if (string_to_ipv4(param_value, net_config->eth0_ipv4_netmask))
            {

              setting_para->port1_DHCPV4_FLAG = 0;
              memcpy(setting_para->port1_IPV4_netmask, net_config->eth0_ipv4_netmask, 4);
              write_base_paremeter_to_fs(setting_para);
              w6100_net_status = W6100_INIT;
              MQTT_connet_status = MQTT_DISCONNECT;

              snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nETH0 IPv4 subnet mask has been set to %s\r\n", param_value);
            }
            else
            {
              snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nError: Invalid IPv4 subnet mask format\r\n");
            }
            return pdFALSE;
          }
          else if (strcmp(param_type, "gw") == 0)
          {
            if (string_to_ipv4(param_value, net_config->eth0_ipv4_gw))
            {
              setting_para->port1_DHCPV4_FLAG = 0;
              memcpy(setting_para->port1_IPV4_GW, net_config->eth0_ipv4_gw, 4);
              write_base_paremeter_to_fs(setting_para);
              w6100_net_status = W6100_INIT;
              MQTT_connet_status = MQTT_DISCONNECT;
              snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nETH0 IPv4 gateway has been set to %s\r\n", param_value);
            }
            else
            {
              snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nError: Invalid IPv4 gateway format\r\n");
            }
            return pdFALSE;
          }
          else if (strcmp(param_type, "dns") == 0)
          {
            if (string_to_ipv4(param_value, net_config->eth0_ipv4_dns))
            {
              snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nETH0 IPv4 DNS has been set to %s\r\n", param_value);
            }
            else
            {
              snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nError: Invalid IPv4 DNS format\r\n");
            }
            return pdFALSE;
          }
        }
      }
      // Set IPv6 parameters
      else if (strcmp(action, "ipv6") == 0 && value)
      {
        // 需要再次获取参数类型
        char temp_cmd2[256];
        strcpy(temp_cmd2, pcCommandString);

        token = strtok(temp_cmd2, " ");
        // 跳过 "net"
        token = strtok(NULL, " ");
        // 跳过 "set"
        token = strtok(NULL, " ");
        // 跳过 interface
        token = strtok(NULL, " ");
        // 跳过 action (ipv6)
        token = strtok(NULL, " ");
        // 现在token是参数类型 (lla, gua, gw6, dhcp6)
        char *param_type = token;

        // 获取参数值
        token = strtok(NULL, " ");
        char *param_value = token;

        if (param_type && param_value)
        {
          if (strcmp(param_type, "lla") == 0)
          {
            if (strlen(param_value) == 32)
            {
              for (int i = 0; i < 16; i++)
              {
                char hex_byte[3] = {param_value[i * 2], param_value[i * 2 + 1], '\0'};
                net_config->eth0_lla[i] = (uint8_t)strtol(hex_byte, NULL, 16);
              }
              char lla_str[40];
              ipv6_to_string(net_config->eth0_lla, lla_str, sizeof(lla_str));
              snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nETH0 IPv6 LLA has been set to %s\r\n", lla_str);
            }
            else
            {
              snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nError: Invalid IPv6 LLA format. Please use 32-character hex string\r\n");
            }
            return pdFALSE;
          }
          else if (strcmp(param_type, "gua") == 0)
          {
            if (strlen(param_value) == 32)
            {
              for (int i = 0; i < 16; i++)
              {
                char hex_byte[3] = {param_value[i * 2], param_value[i * 2 + 1], '\0'};
                net_config->eth0_gua[i] = (uint8_t)strtol(hex_byte, NULL, 16);
              }
              char gua_str[40];
              ipv6_to_string(net_config->eth0_gua, gua_str, sizeof(gua_str));
              snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nETH0 IPv6 GUA has been set to %s\r\n", gua_str);
            }
            else
            {
              snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nError: Invalid IPv6 GUA format. Please use 32-character hex string\r\n");
            }
            return pdFALSE;
          }
          else if (strcmp(param_type, "gw6") == 0)
          {
            if (strlen(param_value) == 32)
            {
              for (int i = 0; i < 16; i++)
              {
                char hex_byte[3] = {param_value[i * 2], param_value[i * 2 + 1], '\0'};
                net_config->eth0_gw6[i] = (uint8_t)strtol(hex_byte, NULL, 16);
              }
              char gw6_str[40];
              ipv6_to_string(net_config->eth0_gw6, gw6_str, sizeof(gw6_str));
              snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nETH0 IPv6 gateway has been set to %s\r\n", gw6_str);
            }
            else
            {
              snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nError: Invalid IPv6 gateway format. Please use 32-character hex string\r\n");
            }
            return pdFALSE;
          }
          else if (strcmp(param_type, "dhcp6") == 0)
          {
            if (strcmp(param_value, "on") == 0 || strcmp(param_value, "1") == 0)
            {
              net_config->eth0_en_ipv6_dhcp_flag = 1;
              snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nETH0 IPv6 DHCP has been enabled\r\n");
            }
            else if (strcmp(param_value, "off") == 0 || strcmp(param_value, "0") == 0)
            {
              net_config->eth0_en_ipv6_dhcp_flag = 0;
              snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nETH0 IPv6 DHCP has been disabled\r\n");
            }
            else
            {
              snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nError: Invalid IPv6 DHCP value. Please use on/off or 1/0\r\n");
            }
            return pdFALSE;
          }
        }
      }
    }
    else if (strcmp(interface, "eth1") == 0)
    {
      // Set MAC address
      if (strcmp(action, "mac") == 0 && value)
      {
        if (string_to_mac(value, net_config->eth1_mac))
        {
          char mac_str[18];
          setting_para->port2_use_written_mac = 1;
          memcpy(setting_para->port2_written_mac, net_config->eth1_mac, 6);
          write_base_paremeter_to_fs(setting_para);
          mac_to_string(net_config->eth1_mac, mac_str, sizeof(mac_str));
          snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nETH1 MAC address has been set to %s\r\n", mac_str);
        }
        else
        {
          setting_para->port2_use_written_mac = 0;
          write_base_paremeter_to_fs(setting_para);
          snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nETH1 MAC address is set to auto-generated. Please restart the device.\r\n");
          snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nError: Invalid MAC address format. Please use format: aa:bb:cc:dd:ee:ff\r\n");
        }
        return pdFALSE;
      }
      // Set DHCP
      else if (strcmp(action, "dhcp") == 0 && value)
      {
        if (strcmp(value, "on") == 0 || strcmp(value, "1") == 0)
        {
          net_config->eth1_dhcp_flag = 1;
          setting_para->port2_DHCPV4_FLAG = 1;
          write_base_paremeter_to_fs(setting_para);
          lwip_net_status = LWIP_PROCESS_INIT;
          snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nETH1 DHCP has been enabled\r\n");
        }
        else if (strcmp(value, "off") == 0 || strcmp(value, "0") == 0)
        {
          net_config->eth1_dhcp_flag = 0;
          setting_para->port2_DHCPV4_FLAG = 0;
          write_base_paremeter_to_fs(setting_para);
          lwip_net_status = LWIP_PROCESS_INIT;
          snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nETH1 DHCP has been disabled\r\n");
        }
        else
        {
          snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nError: Invalid DHCP value. Please use on/off or 1/0\r\n");
        }
        return pdFALSE;
      }
      // Set IPv4 parameters
      else if (strcmp(action, "ipv4") == 0 && value)
      {
        // 需要再次获取参数类型
        char temp_cmd2[256];
        strcpy(temp_cmd2, pcCommandString);

        token = strtok(temp_cmd2, " ");
        // 跳过 "net"
        token = strtok(NULL, " ");
        // 跳过 "set"
        token = strtok(NULL, " ");
        // 跳过 interface
        token = strtok(NULL, " ");
        // 跳过 action (ipv4)
        token = strtok(NULL, " ");
        // 现在token是参数类型 (ip, ipmask, gw, dns)
        char *param_type = token;

        // 获取参数值
        token = strtok(NULL, " ");
        char *param_value = token;

        if (param_type && param_value)
        {
          if (strcmp(param_type, "ip") == 0)
          {
            if (string_to_ipv4(param_value, net_config->eth1_ipv4))
            {
              setting_para->port2_DHCPV4_FLAG = 0;
              memcpy(setting_para->port2_IPV4, net_config->eth1_ipv4, 4);
              write_base_paremeter_to_fs(setting_para);
              lwip_net_status = LWIP_PROCESS_INIT;
              snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nETH1 IPv4 address has been set to %s\r\n", param_value);
            }
            else
            {
              snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nError: Invalid IPv4 address format\r\n");
            }
            return pdFALSE;
          }
          else if (strcmp(param_type, "ipmask") == 0)
          {
            if (string_to_ipv4(param_value, net_config->eth1_ipv4_netmask))
            {
              setting_para->port2_DHCPV4_FLAG = 0;
              memcpy(setting_para->port2_IPV4_netmask, net_config->eth1_ipv4_netmask, 4);
              write_base_paremeter_to_fs(setting_para);
              lwip_net_status = LWIP_PROCESS_INIT;
              snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nETH1 IPv4 subnet mask has been set to %s\r\n", param_value);
            }
            else
            {
              snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nError: Invalid IPv4 subnet mask format\r\n");
            }
            return pdFALSE;
          }
          else if (strcmp(param_type, "gw") == 0)
          {
            if (string_to_ipv4(param_value, net_config->eth1_ipv4_gw))
            {
              setting_para->port2_DHCPV4_FLAG = 0;
              memcpy(setting_para->port2_IPV4_GW, net_config->eth1_ipv4_gw, 4);
              write_base_paremeter_to_fs(setting_para);
              lwip_net_status = LWIP_PROCESS_INIT;
              snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nETH1 IPv4 gateway has been set to %s\r\n", param_value);
            }
            else
            {
              snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nError: Invalid IPv4 gateway format\r\n");
            }
            return pdFALSE;
          }
          else if (strcmp(param_type, "dns") == 0)
          {
            if (string_to_ipv4(param_value, net_config->eth1_ipv4_dns))
            {
              snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nETH1 IPv4 DNS has been set to %s\r\n", param_value);
            }
            else
            {
              snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nError: Invalid IPv4 DNS format\r\n");
            }
            return pdFALSE;
          }
        }
      }
      // Set IPv6 parameters
      else if (strcmp(action, "ipv6") == 0 && value)
      {
        // 需要再次获取参数类型
        char temp_cmd2[256];
        strcpy(temp_cmd2, pcCommandString);

        token = strtok(temp_cmd2, " ");
        // 跳过 "net"
        token = strtok(NULL, " ");
        // 跳过 "set"
        token = strtok(NULL, " ");
        // 跳过 interface
        token = strtok(NULL, " ");
        // 跳过 action (ipv6)
        token = strtok(NULL, " ");
        // 现在token是参数类型 (lla, gua, gw6, dhcp6)
        char *param_type = token;

        // 获取参数值
        token = strtok(NULL, " ");
        char *param_value = token;

        if (param_type && param_value)
        {
          if (strcmp(param_type, "lla") == 0)
          {
            if (strlen(param_value) == 32)
            {
              for (int i = 0; i < 16; i++)
              {
                char hex_byte[3] = {param_value[i * 2], param_value[i * 2 + 1], '\0'};
                net_config->eth1_lla[i] = (uint8_t)strtol(hex_byte, NULL, 16);
              }
              char lla_str[40];
              ipv6_to_string(net_config->eth1_lla, lla_str, sizeof(lla_str));
              snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nETH1 IPv6 LLA has been set to %s\r\n", lla_str);
            }
            else
            {
              snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nError: Invalid IPv6 LLA format. Please use 32-character hex string\r\n");
            }
            return pdFALSE;
          }
          else if (strcmp(param_type, "gua") == 0)
          {
            if (strlen(param_value) == 32)
            {
              for (int i = 0; i < 16; i++)
              {
                char hex_byte[3] = {param_value[i * 2], param_value[i * 2 + 1], '\0'};
                net_config->eth1_gua[i] = (uint8_t)strtol(hex_byte, NULL, 16);
              }
              char gua_str[40];
              ipv6_to_string(net_config->eth1_gua, gua_str, sizeof(gua_str));
              snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nETH1 IPv6 GUA has been set to %s\r\n", gua_str);
            }
            else
            {
              snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nError: Invalid IPv6 GUA format. Please use 32-character hex string\r\n");
            }
            return pdFALSE;
          }
          else if (strcmp(param_type, "gw6") == 0)
          {
            if (strlen(param_value) == 32)
            {
              for (int i = 0; i < 16; i++)
              {
                char hex_byte[3] = {param_value[i * 2], param_value[i * 2 + 1], '\0'};
                net_config->eth1_gw6[i] = (uint8_t)strtol(hex_byte, NULL, 16);
              }
              char gw6_str[40];
              ipv6_to_string(net_config->eth1_gw6, gw6_str, sizeof(gw6_str));
              snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nETH1 IPv6 gateway has been set to %s\r\n", gw6_str);
            }
            else
            {
              snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nError: Invalid IPv6 gateway format. Please use 32-character hex string\r\n");
            }
            return pdFALSE;
          }
          else if (strcmp(param_type, "dhcp6") == 0)
          {
            if (strcmp(param_value, "on") == 0 || strcmp(param_value, "1") == 0)
            {
              net_config->eth1_en_ipv6_dhcp_flag = 1;
              snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nETH1 IPv6 DHCP has been enabled\r\n");
            }
            else if (strcmp(param_value, "off") == 0 || strcmp(param_value, "0") == 0)
            {
              net_config->eth1_en_ipv6_dhcp_flag = 0;
              snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nETH1 IPv6 DHCP has been disabled\r\n");
            }
            else
            {
              snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nError: Invalid IPv6 DHCP value. Please use on/off or 1/0\r\n");
            }
            return pdFALSE;
          }
        }
      }
    }
    else
    {
      snprintf(pcWriteBuffer, xWriteBufferLen, "\r\nError: Invalid network interface. Please use eth0 or eth1\r\n");
      return pdFALSE;
    }
  }

  // Command format error (optimized help information)
  snprintf(pcWriteBuffer, xWriteBufferLen,
           "\r\nNetwork CLI Usage:\r\n"
           "  1. Show config:  net show eth0/eth1\r\n"
           "  2. Show all:     net show all\r\n"
           "  3. Set MAC:      net set eth0/eth1 mac <mac_addr>\r\n"
           "  4. Set DHCP:     net set eth0/eth1 dhcp on/off\r\n"
           "  5. Set IPv4:     net set eth0/eth1 ipv4 <ip|ipmask|gw|dns> <value>\r\n"
           "  6. Set IPv6:     net set eth0/eth1 ipv6 <lla|gua|gw6|dhcp6> <value>\r\n"
           "Examples:\r\n"
           "  net show eth0\r\n"
           "  net show all\r\n"
           "  net set eth0 mac 00:11:22:aa:bb:cc\r\n"
           "  net set eth0 dhcp on\r\n"
           "  net set eth0 ipv4 ip 192.168.1.100\r\n"
           "  net set eth0 ipv4 ipmask 255.255.255.0\r\n"
           "  net set eth0 ipv4 gw 192.168.1.1\r\n"
           "  net set eth0 ipv6 lla fe80000000000000021122fffe334455\r\n");
  return pdFALSE;
}
/* ----------------  CLI Registration Table (end already included)  ---------------- */
const CLI_Command_Definition_t xNetworkCommand = {
    "net",
    "\r\n\r\nnet <show|set> ...     # View / modify network parameters\r\n\r\n",
    network_cli_command,
    -1};
